package com.czk.yygh.msm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.czk.vo.msm.MsmVo;
import com.czk.yygh.msm.config.ConstantPropertiesUtils;
import com.czk.yygh.msm.config.RongLianYunSmsConstant;
import com.czk.yygh.msm.service.MsmService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/11/22 20:56
 */
@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public boolean send(String phone, String code) {
        if (StringUtils.isEmpty(phone)) {
            // 失败
            return false;
        }
        return ronglianyunSmService(code, phone);
    }

    /**
     * mq发送短信的封装
     *
     * @param msmVo
     * @return
     */
    @Override
    public boolean send(MsmVo msmVo) {
        System.out.println("开始发送短信");
        if (msmVo.getPhone() != null) {
            Map<String, Object> param = msmVo.getParam();
            String string = JSONObject.toJSONString(param);
            // 发送容联云短信
            boolean b = ronglianyunSmService(string, msmVo.getPhone());
            return b;
        }
        return false;
    }



    /**
     * 容联云的短信服务
     *
     * @param code
     * @param phone
     * @return
     */
    private boolean ronglianyunSmService(String code, String phone) {
        CCPRestSmsSDK ccpRestSmsSDK = new CCPRestSmsSDK();
        ccpRestSmsSDK.setAccount(RongLianYunSmsConstant.ACCOUNT_SID, RongLianYunSmsConstant.AUTH_TOKEN);
        ccpRestSmsSDK.setAppId(RongLianYunSmsConstant.APPID);
        ccpRestSmsSDK.setBodyType(BodyType.Type_JSON);
        ccpRestSmsSDK.init(RongLianYunSmsConstant.URL, RongLianYunSmsConstant.PORT);
        // 数据，有效分钟数
        String[] datas = {code, "3"};
        HashMap<String, Object> result = ccpRestSmsSDK.sendTemplateSMS(phone, "1", datas);
        System.out.println(result);
        if ("000000".equals(result.get("statusCode"))) {
            // 正常返回输出data包体信息（map）
            HashMap<String, Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for (String key : keySet) {
                Object object = data.get(key);
                System.out.println(key + " = " + object);
            }
            return true;
        } else {
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
            return false;
        }
    }

    /**
     * 阿里云短信服务
     *
     * @param phone
     * @param code
     * @return
     */
    private boolean aliyunSms(String phone, String code) {
        // 整合阿里云的短信服务
        // 设置相关参数
        DefaultProfile profile = DefaultProfile.
                getProfile(ConstantPropertiesUtils.REGION_ID,
                        ConstantPropertiesUtils.ACCESS_KEY_ID,
                        ConstantPropertiesUtils.SECRECT);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        // 设置http方式
        // request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        // 手机号
        request.putQueryParameter("PhoneNumbers", phone);
        // 签名名称->要与你申请的模板名称一致
        request.putQueryParameter("SignName", "我的尚医通");
        // 模板code—>申请的模板code
        request.putQueryParameter("TemplateCode", "SMS_180051135");
        // 验证码，使用json格式
        Map<String, Object> param = new HashMap<>();
        // 生成的验证码
        param.put("code", code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));
        // 调用方法进行短信发送
        try {
            // 由于目前个人无法申请阿里云短信服务，所以这了不做请求了，戴氏代码放这里
            // CommonResponse response = client.getCommonResponse(request);
            // System.out.println(response.getData());
            // return response.getHttpResponse().isSuccess();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
